(function() {
  "use strict";
  APP.Views.UserListRowView = Backbone.View.extend({
    tagName: 'tr',
    events : {
    },
    initialize: function(params) {
      this.index = params.index;
      this.user = params.user;
      this.groups = params.groups;
    },
    setActiveSuccess: function(data) {
      this.user.active = data.active;
    },
    setUserGroupSuccess: function(data) {
      
    },
    beError: function() {
      this.render(true);
    },
    attachEvents: function() {
      var self = this;
      var callbackSetActive = {
        obj: this,
        name: 'setActiveSuccess'
      }
      var callbackSetUserGroup = {
        obj: this,
        name: 'setUserGroupSuccess'
      }
      var callbackError = {
        obj: this,
        name: 'beError'
      }
      this.$('input.activeCheckbox').on('ifChecked', function() {
        APP.BeClient.setActive(self.user.id, 1, callbackSetActive, callbackError);
      });
      this.$('input.activeCheckbox').on('ifUnchecked', function() {
        APP.BeClient.setActive(self.user.id, 0, callbackSetActive, callbackError);
      });
      this.$('.group-select').on('change', function(e) {
        APP.BeClient.setUserGroup(self.user.id, e.val, callbackSetUserGroup, callbackError);
      });
    },
    render: function(reload) {
      if (reload == undefined)
        reload = false;
      this.$el.html(_.template($('#userListRowTemplate').html(), {
        index: this.index,
        user: this.user,
        groups: this.groups
      }));

      this.$('.group-select option[value="'+this.user.group.id+'"]').attr('selected', true);
      this.$('.group-select').select2({
        width: 160
      });
      this.$('input.activeCheckbox').iCheck({
        checkboxClass: 'icheckbox_brands-vgp',
        radioClass: 'iradio_brands-vgp',
        increaseArea: '20%' // optional
      });
      if (!reload)
        this.attachEvents();
      
      return this;
    }
  });
}());